'''
Created on 8 Nov 2014

@author: User
'''
from main.data import mylog, basic_ind
from main.data.my_enums import TradeType


class Trade:
    '''
    classdocs
    '''


    def __init__(self, stock=None):
        '''
        Constructor
        '''
        self.stopPrice = 0;
        self.targetPrice = 0;
        self.opentime= None;
        self.closeTime  = None;
        self.timeLimit = None;
        self.openPrice = None;
        self.closePrice = None;
        self.tradeType = TradeType.LONG;
        self.endCondition = "Trade Not Executed";
        self.closed = False;
        self.tradeLength = -1;
        self.pos=-1;
        
    def check(self,prices):
        mylog.info("Position: " + str(self.pos));
        closes = prices.closes[:self.pos];
        opens = prices.opens[:self.pos];
        highs = prices.highs[:self.pos];
        lows = prices.lows[:self.pos];
        datestrings = prices.datestrings[:self.pos];
        
        mylog.info("First Date :{0} Last Date: {1}".format(datestrings[0],datestrings[-1]));
        mylog.info("Trade Begun on: {0} Price: {1} Target: {2} Stop Loss {3}".format(self.opentime,self.openPrice,self.targetPrice,self.stopPrice));
        
        count=0;
        
        for i in range(len(closes)-1,-1,-1):
            count+=1;
            mylog.debug("Checking Trade for Date: " + datestrings[i] + " Open Price: " +str(opens[i]));
            # If Long
            if self.tradeType == TradeType.LONG:
                # Check Open Price
                if opens[i] <= self.stopPrice:
                    self.__finishTrade(opens[i], "Stopped Out at Open", datestrings[i],count)
                    return;
                elif opens[i] >= self.targetPrice:
                    self.__finishTrade(opens[i], "Reached Target at Open", datestrings[i],count)
                    return;
                # Check Low
                elif lows[i] <= self.stopPrice:
                    self.__finishTrade(lows[i], "Stopped Out at Low", datestrings[i],count)
                    return;
                #Check Highs
                elif highs[i] >= self.targetPrice:
                    self.__finishTrade(lows[i], "Reached Target at High", datestrings[i],count)
                    return;
                #Check Time Limit
                elif count >= self.timeLimit:
                    self.__finishTrade(lows[i], "Reached Time Limit", datestrings[i],count)
                    return;
        
        if self.closed == False:
            mylog.error("Trade Not Closed")
                    
                    
    def __finishTrade(self,price,condition,date,length):
        self.closePrice = price;
        self.endCondition=condition;
        self.closeTime = date;
        self.closed = True;
        self.tradeLength=length;
        #mylog.info("Trade Finished: " + condition);
                    
    def getProfitPerDay(self):
        profit = self.getProfitLoss();
        #mylog.info("Trade length: " + str(self.tradeLength));
        return profit / self.tradeLength;
        
    def getProfitLoss(self):
        if self.openPrice == None or self.closePrice == None:
            mylog.warn("Tried to get profit/loss when trade not finished");
        return basic_ind.pctGainLoss(self.openPrice, self.closePrice);
        
        
        
        
        